﻿using System;
using System.Collections.Generic;
using GSearch;
using LoveMusic.Models;
namespace LoveMusic.Helpers
{
    public static class UpdateMp3Links
    {
        public static void Update()
        {
            Mp3FileApi mp3FileApi = new Mp3FileApi();
            using (ModelDataContext db = new ModelDataContext())
            {
                foreach (Story story in db.Stories)
                {
                    if (story.MP3FILEs.Count == 0)
                    {// add mp3 files
                        List<String> fileUrl = mp3FileApi.GetMp3Urls(story.ArtistName, story.Title);

                       #region mp3Files

                       int counterMp3 = 1;

                       foreach (string mp3Url in fileUrl)
                       {
                           MP3FILE mp3FileObject = new MP3FILE
                           {
                               Story_ID = story.ID,
                               url = mp3Url,
                               IsValid = true,
                               Title = story.Title + " - " + story.ArtistName,
                               DateCreated = DateTime.Now,
                               Count = counterMp3
                           };
                           db.MP3FILEs.InsertOnSubmit(mp3FileObject);
                           counterMp3++;
                       }
                       db.SubmitChanges();

                       #endregion
                    }
                }
            }
        }
    }
    
    public class UpdateGoogleImages
    {
        public void updateGoogleImages()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                GImageSearch gis = new GImageSearch();

                ImageSearchArgs isa = new ImageSearchArgs();

                foreach (ARTIST artist in db.ARTISTs)
                {
                    List<IMAGE> listaSliki = new List<IMAGE>();
                    if (artist.IMAGEs.Count == 0)
                    {
                        isa.Terms = artist.NAME + " music";
                        isa.ImageType = SearchImageType.Photo;

                        GImageResponse resp = gis.Search(isa);

                        if (resp.Response.Results.Length > 0)
                        {
                            for (int i = 0; i < resp.Response.Results.Length; i++)
                            {
                                GImageResults result = resp.Response.Results[i];

                                IMAGE googleImage = new IMAGE();
                                googleImage.ARTIST_IDZ = artist.ARTIST_IDZ;
                                googleImage.HEIGHT = result.Height;
                                googleImage.WIDTH = result.Width;
                                googleImage.URL = result.Url;
                                googleImage.URLTHUMB = result.TbUrl;

                                if (i == 0)
                                    googleImage.TYPE = "primary";
                                else
                                    googleImage.TYPE = "secondary";
                                listaSliki.Add(googleImage);

                                if (i > 10)
                                    break;
                            }
                            db.IMAGEs.InsertAllOnSubmit(listaSliki);
                            db.SubmitChanges();
                        }
                        
                    }
                    
                }
                

                #region old

                //foreach (ARTIST artist in db.ARTISTs)
                //{
                //    List<IMAGE> listaSliki = new List<IMAGE>();
                //    if (artist.IMAGEs.Count == 0)
                //    {
                //        string formattedQuery = Regex.Replace(artist.NAME+" music", @"\s{1,}", "+");
                //        LoveMusic.Helpers.Google.API.ImageSearch.SearchResponse response = LoveMusic.Helpers.Google.API.ImageSearch.SearchService.SearchImages(formattedQuery, 1, 10, true);
                //        if (response.Results.Length != 0)
                //        {
                //            string firstImageUrl = response.Results[0].ImageUrl;
                //            int brojac = 0;
                //            foreach (SearchResult result in response.Results)
                //            {
                //                IMAGE googleImage = new IMAGE();
                //                googleImage.IMAGE_ID = Guid.NewGuid();
                //                googleImage.ARTIST_IDZ = artist.ARTIST_IDZ;
                //                googleImage.HEIGHT = result.ImageHeight;
                //                googleImage.WIDTH = result.ImageWidth;
                //                googleImage.URL = result.ImageUrl;
                //                googleImage.URLTHUMB = result.ThumbnailUrl;

                //                if (brojac == 1)
                //                    googleImage.TYPE = "primary";
                //                else
                //                    googleImage.TYPE = "secondary";
                //                brojac++;
                //                listaSliki.Add(googleImage);
                //            }
                //        }
                //        // FlickrAPI.ApiKey = "42b3068acd34c844f7d647b2a7b657a4";
                //        // listaSliki = FlickrAPI.GetImages(artist.NAME, artist.ARTIST_IDZ);


                //    }
                //    db.IMAGEs.InsertAllOnSubmit(listaSliki);
                //}
                //db.SubmitChanges();

                #endregion

            }
        }

    }

    public class UpdateYouTubeLinks
    {
        public void updateYouTubeDescription()
        {
            YouTubeApi youTubeApi = new YouTubeApi();
            using (ModelDataContext db = new ModelDataContext())
            {
                string encodingCode = string.Empty;
                string videoID = string.Empty;
                string tags = string.Empty;
                foreach (Story story in db.Stories)
                {
                    string youTubeDescription = string.Empty;
                    youTubeApi.GetYouTubeVideo(story.Title, story.ArtistName, ref encodingCode, ref videoID, ref tags, ref youTubeDescription);
                    story.YouTubeID = videoID;
                    //story.youTubeDescription = youTubeDescription;
                }
                db.SubmitChanges();
            }
        }

        public void start()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                foreach (Story story in db.Stories)
                {
                    string youTubeEnc = string.Empty;
                    //if (story.ID > 478)
                    //{
                    //    string youTubeEnc = string.Empty;
                    //    if (story.YouTube != null && story.YouTube.Length > 5 && story.YouTube.Substring(0, 4) == "<div")
                    //    {
                    //        youTubeEnc = story.YouTube;
                    //        string youTubeIDKeBide = youTubeEnc.Substring(14, 11);
                    //        story.YouTubeID = youTubeIDKeBide;
                    //    }
                    //}
                }
                db.SubmitChanges();
            }
        }
    }

    /// <summary>
    /// The point of this class should be to grab all artist name from story and 
    /// get the info from the 2 apis and fill information in the artist table
    /// </summary>
    public class UpdateArtistInformation
    {
        public void startz()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                foreach (Story story in db.Stories)
                {
                    if (db.GetArtistByName(story.ArtistName) == null)
                    {

                        DISCOGZ disco = new DISCOGZ();
                        disco.API_KEY = "c09e193fa7";
                        DiscogsAPI.ResponseClasses.ArtistRequest.artist art = disco.FindArtist(story.ArtistName);
                        int counter = 0;
                        if (art != null)
                        {
                            ARTIST artist = new ARTIST();
                            artist.NAME = art.name;

                            
                            if (art.aliases != null)
                            {
                                foreach (string alias in art.aliases)
                                {
                                    artist.aliases = artist.aliases + "|" + alias;
                                    counter++;
                                    if (counter > 15)
                                    {
                                        counter = 0;
                                        break;
                                    }
                                }
                            }

                            if (art.namevariations != null)
                            {
                                foreach (string nameVar in art.namevariations)
                                {
                                    artist.namevariations = artist.namevariations + "|" + nameVar;
                                    counter++;
                                    if (counter > 10)
                                    {
                                        counter = 0;
                                        break;
                                    }
                                }
                            }
                            artist.REALNAME = art.realname;

                            if (art.urls != null)
                            {
                                foreach (string uri in art.urls)
                                {
                                    artist.URLS = artist.URLS + "|" + uri;
                                    counter++;
                                    if (counter > 10)
                                    {
                                        counter = 0;
                                        break;
                                    }
                                }
                            }
                            if (story.ID == 424)
                            {
                                artist.REALNAME = "U2";
                                artist.namevariations = string.Empty;
                            }

                            story.ARTIST_IDZ = artist.ARTIST_IDZ;
                            db.ARTISTs.InsertOnSubmit(artist);

                            List<IMAGE> listaSliki = disco.GetImages(artist.NAME, artist.ARTIST_IDZ);
                            db.IMAGEs.InsertAllOnSubmit(listaSliki);

                        }


                        else
                        {// Create the artist

                            ARTIST artist = new ARTIST();
                            story.ARTIST_IDZ = artist.ARTIST_IDZ;
                            artist.NAME = story.ArtistName;
                            db.ARTISTs.InsertOnSubmit(artist);
                        }

                        db.SubmitChanges();
                    }
                    else
                    {
                        story.ARTIST_IDZ = db.GetArtistByName(story.ArtistName).ARTIST_IDZ;
                    }
                }
            }
        }


        public void updateArtistGuid()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                foreach (Story story in db.Stories)
                {

                }
            }
        }

    }
}